package com.steel.yfmall.cloud.assistant.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.api.stock.WlFreightServiceAPI;
import com.steel.yfmall.cloud.assistant.bean.param.APIParamFreightBean;
import com.steel.yfmall.cloud.assistant.bean.result.APIResultFreightBean;
import com.steel.yfmall.cloud.assistant.utils.kd.CosmicLoginUtil;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @program: WlFreightServiceAPIImpl
 * @author: liuzhijun
 * @date: 2021-04-03 17:29
 **/
@Service
@Slf4j
public class WlFreightServiceAPIImpl implements WlFreightServiceAPI {


    @Override
    public APIMsgBean<APIResultFreightBean> getFreightMoney(APIParamFreightBean apiParamFreightBean) {
        APIMsgBean<APIResultFreightBean> resultFreightBeanAPIMsgBean = new APIMsgBean<>();
        if (apiParamFreightBean == null) {
            resultFreightBeanAPIMsgBean.setData(null);
            resultFreightBeanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("参数不能为空"));
            return resultFreightBeanAPIMsgBean;
        }
        if (StringUtils.isBlank(apiParamFreightBean.getEnd()) || StringUtils.isBlank(apiParamFreightBean.getStart())
                || apiParamFreightBean.getQty() == null || StringUtils.isBlank(apiParamFreightBean.getIsVote())) {
            resultFreightBeanAPIMsgBean.setData(null);
            resultFreightBeanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("起运地、到货地、是否一票、运量参数 都不能为空"));
            return resultFreightBeanAPIMsgBean;
        }

        String result = "";
        String url = "";
        try {
            SimpleDateFormat formatData = new SimpleDateFormat("yyyy/MM/dd");

            //获取登录token
            String accessToken = CosmicLoginUtil.getAccessToken(CosmicLoginUtil.GetAPPToken());
            url = DS_REQUEST_WL_FREIGHT_URL + "?access_token=" + accessToken;
            String str = "";
            JSONObject obj = new JSONObject();
            obj.put("start", apiParamFreightBean.getStart());
            obj.put("end", apiParamFreightBean.getEnd());
            obj.put("qty", apiParamFreightBean.getQty());
            obj.put("isVote", apiParamFreightBean.getIsVote());
            obj.put("daterange", formatData.format(new Date()));
            str = obj.toString();
            result = CosmicLoginUtil.GetHttpMsg(str, url);
        } catch (Exception e) {
            log.info("请求url异常:" + url);
        }
        JSONObject jsonObject = JSON.parseObject(result);
        Boolean bool = (Boolean) jsonObject.get("success");
        APIResultFreightBean apiResultFreightBean = new APIResultFreightBean();
        if (jsonObject.get("data") != null) {
            apiResultFreightBean = JSON.parseObject(jsonObject.get("data").toString(), APIResultFreightBean.class);
        }
        if (bool) {
            resultFreightBeanAPIMsgBean.setData(apiResultFreightBean);
            resultFreightBeanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
            return resultFreightBeanAPIMsgBean;
        } else {
            resultFreightBeanAPIMsgBean.setData(null);
            resultFreightBeanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean(apiResultFreightBean.getMessage()));
            return resultFreightBeanAPIMsgBean;
        }
    }

    @Value("${ds_request_wl_freight_url}")
    private String DS_REQUEST_WL_FREIGHT_URL;


}
